package net.asfun.jvalog.control;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.service.TagService;
import net.asfun.jvalog.util.RequestUtil;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.mvc.RequestMapping;

public class TagController {

	@RequestMapping("/tag")
	public String index(HttpServletRequest req, Map<String, Object> model) {
		model.put("tags", TagService.listAll());
		return "tags.html";
	}
	
	@RequestMapping("/tag/delete")
	public String delete(HttpServletRequest req, Map<String, Object> model) {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for(int i=0; i<ids.length; i++) {
			TagService.delete(ids[i]);
		}
		return ResponseUtil.feedback("tag.delete", req, model);
	}
	
	@RequestMapping("/tag/update")
	public String update(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		String name = RequestUtil.getRequiredParam(req, "name");
		String slogan =  RequestUtil.getRequiredParam(req, "slogan");
		TagService.update(id, name, slogan);
		return ResponseUtil.feedback("tag.update", req, model);
	}
	
	@RequestMapping("/tag/edit")
	public String edit(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		model.put("tag", TagService.getById(id));
		return "tag.html";
	}
}
